package org.ainlolcat.ainscience.statistics.handlers;

import org.ainlolcat.ainscience.UI.renderer.LineDataRenderer;
import org.ainlolcat.ainscience.UI.renderer.LineRendererSource;
import org.ainlolcat.ainscience.column.Column;
import org.ainlolcat.ainscience.entity.Entity;
import org.ainlolcat.ainscience.statistics.StatisticHandler;
import org.ainlolcat.ainscience.statistics.StatisticStorage;
import org.ainlolcat.ainscience.utils.ArraysUtil;
import org.ainlolcat.ainscience.utils.XMLUtils;
import org.jdom.Element;

import javax.swing.*;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;

/**
 * @author ainlolcat
 */
public class PulseAvgLevelStatisticHandler implements StatisticHandler , LineRendererSource{
    Hashtable<String, double[][]> data = new Hashtable<String, double[][]>();

    public JPanel getRenderer() {
        return new LineDataRenderer(this);
    }

    public Hashtable<String,String> getStatistic(Entity entity, Integer columnID) {
        return getStatistic(entity, columnID, null);
    }

    public Hashtable<String, String> getStatistic(Entity entity, Integer columnID, Element params) {
        Hashtable<String,String> stats = new Hashtable<String, String>();
        Column cy = entity.getColumnByOrder(columnID);
        if (cy.isNumeric()){
            Column cx = entity.getXColumn(cy.getAxis());
            if (cx.isNumeric()){
                double[] y = cy.getDoubleRepresentation();
                double max = ArraysUtil.getMax(y);
                String triggerName = XMLUtils.getSafeString4Children(params, "trigger", null);
                double[] trigger = triggerName != null ? entity.getColumnByName(triggerName).getDoubleRepresentation() : null;
                double triggerMax = trigger!=null? ArraysUtil.getMax(trigger):0;
                //try retrieve settings from params
                String name = XMLUtils.getSafeString4Attribute(params, "name", "PulseAvgLevel");
                int count = 0;
                double sum = 0;
                for (int i=0;i<y.length;i++){
                    if (y[i] > max/2 && (trigger==null || trigger!=null && trigger[i]>triggerMax/2)){
                        sum += y[i];
                        count++;
                    }
                }
                Double answer = sum/count;
                StatisticStorage.getInstance().registerHandler(entity,name,this);
                stats.put(name, answer.toString());
            }
        }
        return stats;
    }

    private double[][] split(double[] x, double[] y){
        double[][] result = new double[3][y.length/2];
        for (int i=0;i<y.length/2;i++){
            result[0][i] = (x[2*i] + x[2*i+1])/2;
            result[1][i] = (y[2*i] + y[2*i+1])/2;
            result[2][i] = (y[2*i] - y[2*i+1])/2;
        }
        return result;
    }

    public Collection<String> getNames() {
        return data.keySet();
    }

    public double[][] getData(String name) {
        return data.get(name);
    }
}
